import cookies from 'vue-cookie';

/**
 * 设置token 如果传过来是空 就直接删除 token
 * @param token
 */
export const setToken = (token) => {
  token ? cookies.set('token',token) : cookies.delete('token');
};
/**
 * 获取token
 * @returns {*}
 */
export const getToken = () => {
  return cookies.get('token');
};

/**
 * 存储数据
 * @param key
 * @param val
 */
export const setItem = (key,val) => {
  /**
   * value 分为两种数据类型
   * 基本类型和复杂数据类型
   */
  if(typeof val === 'object'){
    val = JSON.stringify(val);
  }
  window.localStorage.setItem(key,val);
};

/**
 * 获取数据
 * @param key
 * @returns {string|any}
 */
export const getItem = (key) => {
  const data = window.localStorage.getItem(key);
  try {
    return JSON.parse(data);
  }catch (e){
    return data;
  }
};

export const removeItem = (key) => {
  window.localStorage.removeItem(key);
};

export const removeAll = () => {
  window.localStorage.clear();
};